package com.ysw.sql;

import com.ysw.config.Function;
import com.ysw.config.MapperBean;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

public class MyConfig {
    private ClassLoader loader =ClassLoader.getSystemClassLoader();


    public Connection bulid(){
        try{
            InputStream stream =loader.getResourceAsStream("SqlMapConfig.xml");
            SAXReader reader =new SAXReader();
            Document document =reader.read(stream);
            Element element =document.getRootElement();
            System.out.println("------读取数据库成功-------");
            return evalDataSource(element);
        } catch (DocumentException e) {
            System.out.println("------读取数据库失败--------");
            throw new RuntimeException("--------读取失败------------");
        }
    }

    private Connection evalDataSource(Element element) {
        String driverClassName = null;
        String url =null;
        String username = null;
        String password = null;

        for (Object o:element.elements("property")){
            Element e =(Element) o;
            String value =getValue(e);
            String name =e.attributeValue("name");
            if(name == null){
                throw new RuntimeException("--------读取property信息失败---------");
            }

    switch (name){
        case "driverClassName":
            driverClassName=value;
            break;
        case "url":
            url=value;
            break;
        case "username":
            username=value;
            break;
        case "password":
            password=value;
            break;
        default:
            throw new RuntimeException("--------读取7种基础属性时出错--------");
    }
        }
        //连接数据库
        try{
            Class.forName(driverClassName);
            System.out.println("-------连接数据库获取driver成功-------");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("-------连接数据库获取driver失败---------");
        }
        Connection connection =null;
        //建立数据库的连接
        try{
            connection =DriverManager.getConnection(url,username,password);
            System.out.println("-------url，username，password检验成功--------");
        } catch (SQLException throwables) {
            throw new RuntimeException("------url，username，password不对应--------");
        }
        return connection;

    }

    private String getValue(Element e) {
        if(e.hasContent()){
            return e.getText();
        }else{
            return e.attributeValue("value");
        }
    }
    public MapperBean readMapper(String path){
        MapperBean mapperBean = new MapperBean();
        try{
            InputStream stream = loader.getResourceAsStream(path);
            SAXReader reader = new SAXReader();
            Document document = reader.read(stream);
            Element root = document.getRootElement();
            //存储mapper节点的namespace的值
            mapperBean.setInterfaceName(root.attributeValue( "namespace" ).trim());
            //存储方法
            ArrayList<Function> list = new ArrayList<>();
            //遍历根节点下的子节点
            for (Iterator iterator = root.elementIterator(); iterator.hasNext(); ){
                Function function =new Function();
                Element element = (Element) iterator.next();
                String sqlType=element.getName().trim();
                String functionName = element.attributeValue("id" ).trim();
                String sql = element.getText().trim();
                String resultType = null;
                Object newInstance = null;
                if (null != element.attributeValue("resultType ")){
                    resultType = element.attributeValue("resultType").trim();
                    try{
                        newInstance = Class.forName(resultType). newInstance();
                    }catch (InstantiationException e){
                        e.printStackTrace();
                    }catch (IllegalAccessException e){
                        e.printStackTrace();
                    }catch (ClassNotFoundException e){
                        e.printStackTrace();
                    }
                    function.setResultType(newInstance);
                }
                function.setSqlType(sqlType);
                function.setFunctionName(functionName);
                function.setSql(sql);
                list.add(function);
            }
            //存储方法
            mapperBean.setList(list);
        }catch (DocumentException e){
            e.printStackTrace();
        }
        return mapperBean;
    }

}
